<?php
namespace app\admin\controller;

use app\common\models\AdminUser as AdminUserModel;

/**
 * @name 管理员管理
 * @desc 管理员管理
 * @auth true
 * @author: xujianping(814876569@qq.com)
 * @datetime: 2017/9/18 上午 10:37
 */
class AdminUser extends Common
{
    /**
     * index
     * @author: xujianping(814876569@qq.com)
     * @return mixed
     */
    public function index(){

        $adminUser = new AdminUserModel();
        $list = $adminUser->getPageData();

        $this->assign('list',$list);
        return view();
    }

    /**
     * add
     * @author: xujianping(814876569@qq.com)
     * @return mixed
     */
    public function add(){
        if ($this->request->isPost()){
            $post = $this->request->post();
            $adminUser = new AdminUserModel();
            $res = $adminUser->addDataByField($post);
            if ($res){
                $this->success('添加成功','/admin/admin_user');
            }else{
                $this->error('添加失败');
            }
        }else{
            return view();
        }
    }

    /**
     * edit
     * @author: xujianping(814876569@qq.com)
     * @return \think\response\View
     */
    public function edit(){

        $adminUser = new AdminUserModel();

        if ($this->request->isPost()){
            $post = $this->request->post();

            unset($post['confirm_password']);
            if (isset($post['password']) && $post['password'] == ""){
                unset($post['password']);
            }
            $id = 0;
            if (isset($post['id']) && $post['id'] > 0){
                $id = $post['id'];
            }

            $res = $adminUser->updateDataById($id,$post);
            if ($res !== false){
                $this->success('更新成功','/admin/admin_user');
            }else{
                $this->error('更新失败');
            }
        }else{
            $id = $this->request->get('id',0);
            $info = $adminUser->getDataById($id);
            if (is_null($info)){
                $this->error('数据获取失败');
            }
            return view('',['info'=>$info]);
        }
    }

    /**
     * 删除
     */
    public function delete(){
        $id = $this->request->post('id',0);
        $adminUser = new AdminUserModel();
        $d = $adminUser->deleteDataById($id);
        if ($d){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}